﻿#region Header

// Majesty of Omega - The next evolution of macro management space strategy
// 
// Copyright 2007-2009 André Claaßen
// 
// This file is protected by the terms and conditions of the
// Microsoft Community License (Ms-CL 1.1), a copy of which should
// have been distributed along with this software. If not,
// you may find the license information at the following URL:
// 
// http://www.microsoft.com/resources/sharedsource/licensingbasics/communitylicense.mspx

#endregion

#region

using System.Windows;
using DesignByContract;

#endregion

namespace Majesty_of_Omega.GUI.Pages.Game.Research.Parts
{
    /// <summary>
    /// Interaction logic for PercentageBar.xaml. The Percentage Bar provides an easy way,to display vertical Bar Charts
    /// </summary>
    public partial class PercentageBar
    {
        /// <summary>
        /// Report, to change the percentage value of the progress bar.
        /// </summary>
        public static readonly DependencyProperty PercentageProperty =
            DependencyProperty.Register("Percentage", typeof (double), typeof (PercentageBar), new UIPropertyMetadata(100.0, OnPercentageValueChanged));


        /// <summary>
        /// Resize the bar, when the percentage value changed
        /// </summary>
        /// <param name="d">The PercentageBar object.</param>
        /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        private static void OnPercentageValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            double newPercentage = (double) e.NewValue;
            PercentageBar percentageBar = (PercentageBar) d;
            percentageBar.ChangeBarToReflectValue(newPercentage);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PercentageBar"/> class.
        /// </summary>
        public PercentageBar()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the percentage of the bar. The Bar adjusts its size to the value
        /// </summary>
        /// <value>The percentage.</value>
        public double Percentage
        {
            get { return (double) GetValue(PercentageProperty); }
            set { SetValue(PercentageProperty, value); }
        }

        /// <summary>
        /// Changes the bar to reflect value.
        /// </summary>
        /// <param name="percentage">The percentage.</param>
        private void ChangeBarToReflectValue(double percentage)
        {
            Require.IsTrue(percentage >= 0.0 && percentage <= 100.0, "The percentage value must be in a range between 0 and 100");
            EmptyRowDef.Height = new GridLength(100.0 - percentage, GridUnitType.Star);
            BarRowDef.Height = new GridLength(percentage,GridUnitType.Star);
        }
    }
}